package com.eqs.enterprisequestionnaire.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eqs.enterprisequestionnaire.mapper.ResponseMapper;
import com.eqs.enterprisequestionnaire.model.pojo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TimeService {
    @Autowired
    ResponseMapper responseMapper;
    @Autowired
    HistoryService historyService;

    //平均完成时间
    public Double averageFinishedTime(Integer questionnaireId) {
        QueryWrapper<Response> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_id", questionnaireId);
        List<Response> responses = responseMapper.selectList(queryWrapper);

        if (responses.isEmpty()) {
            return 0.0;
        }

        double sum = 0.0;
        for (Response r : responses) {
            if(r.getSubmittedTime()!=null) {
                Duration duration = Duration.between(r.getStartTime(), r.getSubmittedTime());
                sum += duration.toMinutes(); // 计算分钟数
            }
        }
        return sum / responses.size();
    }

    //每日时间段分析
    public Map<String, Integer> getResponsesByTimePeriod(Integer questionnaireId) {
        QueryWrapper<Response> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_id", questionnaireId);
        List<Response> responses = responseMapper.selectList(queryWrapper);

        Map<String, Integer> periodCountMap = new HashMap<>();
        for (Response r : responses) {
            if (r.getSubmittedTime() != null) {
                String period = getTimePeriod(r.getStartTime());
                periodCountMap.put(period, periodCountMap.getOrDefault(period, 0) + 1);
            }
        }
        return periodCountMap;
    }

    private String getTimePeriod(LocalDateTime dateTime) {
        int hour = dateTime.getHour();
        if (hour >= 0 && hour < 6) {
            return "00:00-06:00";
        } else if (hour >= 6 && hour < 12) {
            return "06:00-12:00";
        } else if (hour >= 12 && hour < 18) {
            return "12:00-18:00";
        } else {
            return "18:00-00:00";
        }
    }

    public Map<String, Integer> getResponsesByDayOfWeek(Integer questionnaireId) {
        QueryWrapper<Response> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_id", questionnaireId);
        List<Response> responses = responseMapper.selectList(queryWrapper);

        Map<String, Integer> dayCountMap = new HashMap<>();
        for (Response r : responses) {
            if (r.getSubmittedTime() != null) {
                String dayOfWeek = getDayOfWeek(r.getSubmittedTime());
                dayCountMap.put(dayOfWeek, dayCountMap.getOrDefault(dayOfWeek, 0) + 1);
            }
        }

        return dayCountMap;
    }

    private String getDayOfWeek(LocalDateTime submittedTime) {
        DayOfWeek dayOfWeek = submittedTime.getDayOfWeek();
        switch (dayOfWeek) {
            case MONDAY:
                return "Monday";
            case TUESDAY:
                return "Tuesday";
            case WEDNESDAY:
                return "Wednesday";
            case THURSDAY:
                return "Thursday";
            case FRIDAY:
                return "Friday";
            case SATURDAY:
                return "Saturday";
            case SUNDAY:
                return "Sunday";
            default:
                return "Unknown";
        }
    }
}
